package com.sense.kukoo.biz_messages.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.common.repo.biz.MessagesItemBean
import com.sense.kukoo.common.repo.http.bean.UserMessageSystemBean

class MessagesSystemsViewModel: BaseViewModel() {

    private val TAG: String = javaClass.simpleName

    private var mIndex = 2
    private val mPageSize = 10
    private var type: Int = 4
    private var mNetworkRetry: ()->Unit = {}
    private val mMessages = arrayListOf<UserMessageSystemBean>()

    val action_back: MutableLiveData<Boolean> = MutableLiveData()
    val action_networkError: MutableLiveData<Boolean> = MutableLiveData()
    val action_finishRefresh: MutableLiveData<Boolean> = MutableLiveData()
    val action_finishLoadMore: MutableLiveData<Boolean> = MutableLiveData()
    val data_messages: MutableLiveData<ArrayList<MessagesItemBean>> = MutableLiveData()
    val data_enableLoadMore: MutableLiveData<Boolean> = MutableLiveData(false)

    override fun createModel(): BaseMvvmModel? = null

    fun setFromType(t: Int){
        type = t
    }

    override fun initData() {}

    fun onBackClick(){
        action_back.postValue(true)
    }

    fun showSystemsData(){
        val list = arrayListOf<MessagesItemBean>()
        mMessages.forEach {
            it.type = type
            list.add(MessagesItemBean(MessagesItemBean.TYPE_SYSTEMS, system = it))
        }
        data_messages.postValue(list)
        action_finishRefresh.postValue(true)
        action_finishLoadMore.postValue(true)
    }

    fun refreshSystemsData(){
        toView(ACTION_SHOW_DIALOG)
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            mNetworkRetry = {refreshSystemsData()}
            action_networkError.postValue(true)
            toView(ACTION_HIDE_DIALOG)
        }
        request(onError) {
            val result = Repo.http.messageSystems(1, mPageSize, type)
            if (result.isSuccess()) {
                mIndex = 2
                result.data?.let {
                    mMessages.clear()
                    mMessages.addAll(it)
                    if(it.size < mPageSize){
                        data_enableLoadMore.postValue(false)
                    } else {
                        data_enableLoadMore.postValue(true)
                    }
                    showSystemsData()
                }
            } else {
                Log.e(TAG, "result=" + result.msg)
                mNetworkRetry = {refreshSystemsData()}
                action_networkError.postValue(true)
            }
            toView(ACTION_HIDE_DIALOG)
        }
    }

    fun loadMoreSystemsData(){
        toView(ACTION_SHOW_DIALOG)
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            mNetworkRetry = {loadMoreSystemsData()}
            action_networkError.postValue(true)
            toView(ACTION_HIDE_DIALOG)
        }
        request(onError) {
            val result = Repo.http.messageSystems(mIndex, mPageSize, type)
            if (result.isSuccess()) {
                mIndex++
                result.data?.let {
                    mMessages.addAll(it)
                    if(it.size < mPageSize){
                        data_enableLoadMore.postValue(false)
                    }
                    showSystemsData()
                }
            } else {
                Log.e(TAG, "result=" + result.msg)
                mNetworkRetry = {loadMoreSystemsData()}
                action_networkError.postValue(true)
            }
            toView(ACTION_HIDE_DIALOG)
        }
    }

    fun onNetworkRetry(){
        mNetworkRetry()
    }

}